//
//  NSString+Needs.m
//  CToolBox
//
//  Created by crj on 16/8/16.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "NSString+Needs.h"


@implementation NSString (Needs)

+ (NSMutableAttributedString *)attributedStringWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor{
    
    NSRange range = NSMakeRange(0, text.length);
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:range];
    [attributedString addAttribute:NSFontAttributeName value:font range:range];
    return attributedString;
}

- (NSMutableAttributedString *)setSubStringAtColor:(UIColor *)color subStr:(NSString *)subStr{
    
    //    NSString *numberStr = [NSString stringWithFormat:@"%",number];
    NSRange range = [self rangeOfString:subStr];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    return attributedString;
    
}

- (NSMutableAttributedString *)setNumberColorToGrayWithNumber:(long)number{
    NSString *numberStr = [NSString stringWithFormat:@"%ld",number];
    NSRange range = [self rangeOfString:numberStr];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorGray1, 1) range:range];
    return attributedString;
}

- (NSMutableAttributedString *)setStrColorToBlueWithStr:(NSString *)str
{
    NSRange range = [self rangeOfString:str];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorBlue6, 1) range:range];
    return attributedString;
}

- (NSMutableAttributedString *)setStrColorToGrayWithStr:(NSString *)str
{
    NSRange range = [self rangeOfString:str];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorGray3, 1) range:range];
    return attributedString;
}

- (NSMutableAttributedString *)setNumberColorToYellowWithNumber:(long)number{
    NSString *numberStr = [NSString stringWithFormat:@"%ld",number];
    NSRange range = [self rangeOfString:numberStr];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorYellow1, 1) range:range];
    return attributedString;
}

- (NSMutableAttributedString *)setNumberColorToYellowWithFloatNumber:(float)number
{
    NSString *numberStr = [NSString stringWithFormat:@"%.2f",number];
    NSRange range = [self rangeOfString:numberStr];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorYellow1, 1) range:range];
    return attributedString;
}

- (NSMutableAttributedString *)setNumberColorToOrangeWithNumber:(long)number{
    NSString *numberStr = [NSString stringWithFormat:@"%ld",number];
    NSRange range = [self rangeOfString:numberStr];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorOrange2, 1) range:range];
    return attributedString;
}


- (NSMutableAttributedString *)setStrFont:(CGFloat)font
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
     [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, 1)];
    return attributedString;
}


- (NSMutableAttributedString *)setNumberColorToGreenWithNumber:(long)number{
    
    NSString *numberStr = [NSString stringWithFormat:@"%ld",number];
    NSRange range = [self rangeOfString:numberStr];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorGreen1, 1) range:range];
    return attributedString;
}

- (NSMutableAttributedString *)attributedStringWithSubString:(NSString *)subString{
    
    NSRange range = [self rangeOfString:subString];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(kColorOrange2, 1) range:range];
    return attributedString;
}



- (NSMutableAttributedString *)attributedStringWithLineSpace:(CGFloat)lineSpace{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    [paragraphStyle setLineSpacing:lineSpace];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self length])];
    
    return attributedString;
}

- (NSMutableAttributedString *)attributedStringWithLineSpace:(CGFloat)lineSpace textAlignment:(NSTextAlignment)alignment{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = alignment;
    [paragraphStyle setLineSpacing:lineSpace];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self length])];
    
    return attributedString;
}

- (NSMutableAttributedString *)attributedWithIconName:(NSString *)name
                                                color:(UIColor *)color
                                                space:(CGFloat)space
                                             fontSize:(CGFloat)fontSize{
    
    
    NSString *string = [NSString stringWithFormat:@"%@%@",self,name];
    
    
    NSRange iconRange = [string rangeOfString:name];
    NSMutableAttributedString *infoAttributed = [string attributedStringWithLineSpace:space];
    [infoAttributed addAttribute:NSForegroundColorAttributeName value:color range:iconRange];
    [infoAttributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontIcon size:fontSize] range:iconRange];
    
    return infoAttributed;
}

- (NSMutableAttributedString *)attributedStringWithIconCode:(NSString *)iconCode fontSize:(CGFloat)fontSize iconFontSize:(CGFloat)iconFontSize stringColor:(UIColor *)stringColor iconColor:(UIColor *)iconColor{
    
    
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:self];
    if (iconFontSize != fontSize) {
        [attributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontIcon size:fontSize] range:NSMakeRange(0, self.length)];
    }
    
    NSRange iconRange = [self rangeOfString:iconCode];
    [attributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontIcon size:iconFontSize] range:iconRange];
    
    [attributed addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (iconFontSize-fontSize)) range:NSMakeRange(iconCode.length, self.length - iconCode.length)];
    [attributed addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, self.length)];
    [attributed addAttribute:NSForegroundColorAttributeName value:iconColor range:iconRange];
    return attributed;
}

- (NSMutableAttributedString *)attributedStringWithIconCode1:(NSString *)iconCode fontSize:(CGFloat)fontSize iconFontSize:(CGFloat)iconFontSize stringColor:(UIColor *)stringColor iconColor:(UIColor *)iconColor{
    
    
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:self];
    if (iconFontSize != fontSize) {
        [attributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontIcon size:fontSize] range:NSMakeRange(0, self.length)];
    }
    
    NSRange iconRange = [self rangeOfString:iconCode];
    [attributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontIcon size:iconFontSize] range:iconRange];
    
    [attributed addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, self.length)];
    [attributed addAttribute:NSForegroundColorAttributeName value:iconColor range:iconRange];
    return attributed;
}

+(NSString *)setShowStyleWithNumber:(long)number{
    
    NSString *numberStr;
    if (number < 10000) {
        numberStr = [NSString stringWithFormat:@"%ld",number];
    }else if (number < 100000){
        double numM = (number * 1.0) / 10000;
        numberStr = [NSString stringWithFormat:@"%.1f万",numM];
    }else if (number < 100000000){
        number = number/10000;
        numberStr = [NSString stringWithFormat:@"%ld万",number];
    }else if (number < 1000000000){
        double numB = (number * 1.0) / 100000000;
        numberStr = [NSString stringWithFormat:@"%.1f亿",numB];
    }else{
        number = number/100000000;
        numberStr = [NSString stringWithFormat:@"%ld亿",number];
    }
    
    return numberStr;
    
}

+(NSString *)setShowOnlineCountWithNumber:(long)number{
    
    NSString *numberStr;
    if (number < 5000) {
        numberStr = [NSString stringWithFormat:@"%ld",number];
    }else {
        double numM = (number * 1.0) / 10000;
        numberStr = [NSString stringWithFormat:@"%.1f万+",numM];
    }
    
    return numberStr;
    
}

- (NSString *)iconValueEncoding{
    
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[self dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        
    NSString*string = [attrStr string];
    return string;
}

- (NSString *)specialCharactersEncode{

    NSString *string = @"";
    string = [self stringByReplacingOccurrencesOfString:@"\n" withString:@"$n"];
    string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@"$r"];
    return string;
}

- (NSString *)specialCharactersDecode{

    NSString *string = @"";
    string = [self stringByReplacingOccurrencesOfString:@"$n" withString:@"/n"];
    string = [string stringByReplacingOccurrencesOfString:@"$r" withString:@"/r"];
    return string;
}


@end
